package com.wyp168.jse.face.question;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @ClassName ValidatePersonInfo
 * @Title 1.验证信息
 * @Description 网页表单数据提交时，我们需要对用户输入的数据进行验证。提供一个表单验证类，能够对常见的表单输入文本进行验证，如用户姓名、邮箱、手机号、生日日期、上传文件大小等等。
 * @Author wyp168
 * @Date 2023/3/1 19:56
 */
public class ValidatePersonInfo {
    public static void main(String[] args) {
        ValidatePersonInfo validatePersonInfo = new ValidatePersonInfo();

//        System.out.println(validatePersonInfo.validPhoneNum("111111111"));    //false
//        System.out.println(validatePersonInfo.validBirthday(new Date())); //true
//        System.out.println(validatePersonInfo.validUserName("sdfsdfsdfsdf@##$@$")); //false
//        System.out.println(validatePersonInfo.validEMail("2234234"));   //false


        File file = new File("/Users/wangyuanpeng/com/wyp168/juc/src/main/java/com/wyp168/jse/face/question/file/阿里巴巴java开发手册2020.pdf");

        System.out.println(validatePersonInfo.validFileSize(file)); //false


    }

    //"手机号"正则表达式
    final String PHONE_PATTERN = "0?(13|14|15|17|18|19)[0-9]{9}";
    //"用户姓名"正则表达式
    final String USER_NAME_PATTERN = "[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+";
    //"email邮箱"正则表达式
    final String MAIL_PATTERN = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
    //"出生日期"正则表达式
    final String BIRTHDAY_PATTERN = "\\d{4}(\\-|\\/|.)\\d{1,2}\\1\\d{1,2}";

    /**
     * 验证手机号合法性
     */
    public boolean validPhoneNum(String phoneNum) {
//        return Pattern.compile(PHONE_PATTERN).matcher(phoneNum).matches();
        return isMatchers(phoneNum, PHONE_PATTERN);
    }

    /**
     * 验证用户名合法性
     */
    public boolean validUserName(String userName) {
//        return Pattern.compile(userName).matcher(userName).matches();
        return isMatchers(userName, USER_NAME_PATTERN);
    }

    /**
     * 验证用户邮箱合法性
     */
    public boolean validEMail(String eMailNum) {
        return isMatchers(eMailNum, MAIL_PATTERN);
    }

    /**
     * 验证出生日期合法性
     * @param birthdayDate 入参
     * @return 返回是否合法
     */
    public boolean validBirthday(Date birthdayDate) {
//        System.out.println(birthdayDate.toString());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String birthdayDateString = simpleDateFormat.format(birthdayDate);
        return isMatchers(birthdayDateString, BIRTHDAY_PATTERN);
    }

    /**
     * 验证文件大小是否合法(这里设置为小于5MB为合法)
     * @param file 所要上传的文件
     * @return
     */
    public boolean validFileSize(File file) {
        FileInputStream fis = null;
        float totalSize = 0;
        try {
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int size = -1;
            while ((size = fis.read(bytes)) != -1) {
                totalSize += size;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return totalSize / 1024 / 1024 <= 1;
    }



    public boolean isMatchers(String value, String pattern) {
        return Pattern.compile(pattern).matcher(value).matches();
    }


}
